<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initAutoload()
    {
        // http://framework.zend.com/manual/en/zend.loader.autoloader-resource.html
        $resourceLoader = new Zend_Loader_Autoloader_Resource(array(
            'basePath'      => APPLICATION_PATH . '/modules/user/',
            'namespace'     => 'User',
            'resourceTypes' => array(
                'model' => array(
                    'namespace' => 'Model',
                    'path'      => 'models/',
                ),
                'form'  => array(
                    'namespace' => 'Form',
                    'path'      => 'forms/'
                ),
            )
        ));
        
        // This should be placed in the configuration file, but how? :)
        $moduleLoader = new Zend_Application_Module_Autoloader(array(
            'namespace' => '',
            'basePath' => APPLICATION_PATH . '/modules/default/'
        ));
        
        // Make sure that the autoloader looks in Pldkp for common files
        $autoloader = Zend_Loader_Autoloader::getInstance();
        $autoloader->registerNamespace('Pldkp_');
        
        
        
        return $moduleLoader;
    }
    
    protected function _initRegistry()
    {
        $registry = new Zend_Registry(array(), ArrayObject::ARRAY_AS_PROPS);
        Zend_Registry::setInstance($registry);
        return $registry;
    }
    
    protected function _initUser()
    {
        // Make sure that the db is bootrapped befre attempting to optain the user
        $this->bootstrap('db');
        $this->bootstrap('registry');
        
        // Put the user in the registry 
        // (this could be placed in its own _init function)
        $user = null;
        if (Zend_Auth::getInstance()->hasIdentity()) {
            $indentityData = Zend_Auth::getInstance()->getIdentity();
            $user = User_Model_Users::getById($indentityData->id);
        }
        Zend_Registry::getInstance()->user = $user;
        
        return $user;
    }
    
    protected function _initAcl()
    {
        $this->bootstrap('user');
        $this->bootstrap('registry');
        $this->bootstrap('frontcontroller');
         
        $user = Zend_Registry::getInstance()->user;
        
        // Setup the acl plugin
        $acl = new User_Model_Acl($user);
        $authenticationPlugin = new Pldkp_Controller_Plugin_Authorization($acl);
        $front = Zend_Controller_Front::getInstance();
        $front->registerPlugin($authenticationPlugin);
        Zend_Registry::getInstance()->acl = $acl;
        
        return $acl;
    }
    
    protected function _initNavigation()
    {
        $this->bootstrap('view');
        $this->bootstrap('registry');
        $this->bootstrap('acl');
        
        $view = $this->getResource('view');
        $acl = Zend_Registry::getInstance()->acl;
        
        // Todo: move the navigtaion file name to the config instead
        $navigationConfig = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation.xml', 'navigation');
        $navigationContainer = new Zend_Navigation($navigationConfig);
        
        $view->navigation($navigationContainer)->setAcl($acl)->setRole('user');
    }
    
    protected function _initJquery()
    {
        $this->bootstrap('view');
        $this->bootstrap('frontController');
        
        $view = $this->getResource('view');
        $front = $this->getResource('frontController');
        
        $view->jquery()->setLocalPath($front->getBaseUrl()      . '/js/jquery/jquery-1.3.2.min.js')
                       ->setUiLocalPath($front->getBaseUrl()    . '/js/jquery/jquery-ui-1.7.2.custom.min.js')
                       ->addStylesheet($front->getBaseUrl()     . '/js/jquery/jquery-ui-1.7.2.custom.css')
                       ->addJavascriptFile($front->getBaseUrl() . '/js/jquery/pldkp-common.js')
                       ->addJavascriptFile($front->getBaseUrl() . '/js/jquery/simplemodal/jquery.simplemodal-1.3.3.min.js')
                       ->addStylesheet($front->getBaseUrl()     . '/js/jquery/simplemodal/jquery.simplemodal.css')
                       ->uiEnable()
                       ->enable();
    }
}

